package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import com.codename1.ui.CN;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class OpenGalleryTest2793 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        Button titleCmp = new Button("My Title Button") {
            @Override
            public void pointerPressed(int x, int y) {
                System.out.println("Got press at "+x+","+y); 
                    
                super.pointerPressed(x, y); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void pointerReleased(int x, int y) {
                System.out.println("Got release at "+x+","+y);
                super.pointerReleased(x, y); //To change body of generated methods, choose Tools | Templates.
            }
            
            
            
        };
        titleCmp.addActionListener(l -> {
            Log.p("Title component tapped");
            Dialog.show("Title tapped", "The toolbar title was tapped", "OK", null);
        });
        titleCmp.getAllStyles().setAlignment(Label.LEFT);
        hi.getToolbar().setTitleCentered(false);
        hi.getToolbar().setTitleComponent(titleCmp);

        Button openGalleryBtn = new Button("Open Gallery");
        openGalleryBtn.addActionListener(l -> CN.openGallery(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                if (evt == null || evt.getSource() == null) {
                    Log.p("The user tapped \"CANCEL\" in the Gallery");
                } else {
                    Log.p("The user choosed an image from the gallery");
                }
            }
        }, CN.GALLERY_IMAGE));
        hi.add(openGalleryBtn);
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
